package tasks.leetcode;

public class TaskCountAndSay {
    public static void main(String[] args) {
        TaskCountAndSay taskCountAndSay = new TaskCountAndSay();
        String s = taskCountAndSay.countAndSay(4);
        System.out.println(s);
    }

    public String countAndSay(int n) {// 1121
        String str = "1";
        for (int i = 2; i <= n; i++) {
            StringBuilder stringBuilder = new StringBuilder();
            int start = 0;
            int pos = 0;

            while (pos < str.length()) {
                while (pos < str.length() && str.charAt(pos) == str.charAt(start)) {
                    pos++;
                }
                stringBuilder.append(String.valueOf(pos - start)).append(str.charAt(start));
                System.out.println("sb:" + stringBuilder.toString());
                start = pos;
            }
            str = stringBuilder.toString();

        }
        return str;
    }
}
